/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import sagroup.dracots.DracoTuple;

import lights.interfaces.ITuple;
import lime2.LimeConstants;
import lime2.LimeServerID;
import lime2.Location;

/**
 * @author Xing Jiankuan
 *
 */
public class Util {
	/**
	 * Convert an array of LimeServerID to Location
	 * @param hosts
	 * @return
	 * @author Xing Jiankuan
	 */
	public static Location[] toLocationArray(LimeServerID[] hosts) {
		Location[] ret = new Location[hosts.length];
		for(int i = 0; i < hosts.length; i++) {
			ret[i] = new Location(hosts[i]);
		}
		return ret;
	}
	
	public static String getExceptionDetail(Exception e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		return e.toString() + "\n:Trace:\n" + sw.toString();
	}

	/**
	 * parameter checking function: returns true if the given array is null or
	 * contains at least one null element
	 */
	public static boolean testNullArray(Object[] o) {
		if (o == null)
			return true;
		for (int i = 0; i < o.length; i++)
			if (o[i] == null)
				return true;
		return false;
	}
	
	public static DracoTuple[] convertITupleArrayToDracoTupleArray(
			ITuple[] tuples) throws ClassNotFoundException {
		DracoTuple[] retarr = new DracoTuple[tuples.length];
		for (int i = 0; i < retarr.length; i++) {
			retarr[i] = DracoTuple.create(tuples[i]);
		}
		return retarr;
	}
	
	public static ITuple[] convertDracoTupleArrayToITupleArray(
			DracoTuple[] dts) {
		ITuple[] retarr = new ITuple[dts.length];
		for (int i = 0; i < retarr.length; i++) {
			retarr[i] = dts[i].toLimeWrapper().limifyWrapper();
		}
		return retarr;
	}
	
	/**
	 * return true if op is out or outg
	 * @param op operation type constants
	 * @author Xing Jiankuan
	 */
	public static boolean isWriteOp(byte op) {
		if (op == LimeConstants.OUT || op == LimeConstants.OUTG)
			return true;
		else
			return false;
	}
}
